package com.aiyige.page.my.wallet.utils;

import com.aiyige.BuildConfig;
import com.aiyige.base.api.ApiManager;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.setup.model.Setup;
import com.aiyige.setup.util.SetupUtil;
import com.aiyige.utils.ErrorUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.RetrofitUtil;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WalletUtil {


    public void getServiceNumber(final WalletServiceModel model) {
        ApiManager.getService()
                .getSetup(SetupUtil.GROUP_TYPE_GLOBAL_TYPE, BuildConfig.appVersion, "development")
                .enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            if (!response.isSuccessful()) {
                                throw new Exception(ErrorUtil.extractMessageFromErrorResponse(response));
                            }
                            String json = RetrofitUtil.getResponseBodyAsString(response);
                            List<Setup> setupList = JsonUtil.toObjectList(json, Setup.class);
                            if (!ListUtil.isEmpty(setupList) && setupList.get(0).getData() != null && setupList.get(0).getData().size() > 0) {
                               String serviceMobile =  (String)setupList.get(0).getData().get("serviceMobile");
                                model.setServicePhone(serviceMobile);
                            }
                        } catch (Exception e) {
                            onFailure(call, e);
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
//                        ToastX.show(t.getMessage());
                    }
                });
    }
}
